// component/auth.js
const api = require('../../config/api.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    userInfo:{
      type:Object,
      default:null
    },
    onConfirmId:{
      type:Number,
      default:0,
      observer: function (val) {
        let user = wx.getStorageSync('wxUser') || {}
        if (val) {
          //存在用户信息则退出
          this.setData({
            show: true,
            wxUser: user
          })
        } else {
          this.setData({
            show: false,
            wxUser: user
          })
        }

      }
    },
    showProp: {
      type: Boolean,
      default: false,
      observer: function (val) {
        let user = wx.getStorageSync('wxUser') || {}
        if (val) {
          //存在用户信息则退出
          this.setData({
            show: true,
            wxUser: user
          })
        } else {
          this.setData({
            show: false,
            wxUser: user
          })
        }

      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    wxUser: null,
    wxPhoneCSS: false,
    userInfo: null,
    onConfirmId: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //点击确认按钮的事件
    onConfirm(e) {
      console.log(e,'onConfirm')
      let id = e.currentTarget.dataset.id;
      if (id == 0) {
        this.getUserProfile(e)
      } else {
        // this.getPhoneNumber(e)
      }
    },
    /**
     * 获取手机号 
     */
    getPhoneNumber(e) {
      console.log(e,'getPhoneNumber')
      var this_ = this;
      let openid = wx.getStorageSync('openid');
      let sessionKey = wx.getStorageSync('sessionKey')
      wx.request({
        url: api.phoneNumber,
        method: 'POST',
        data: {
          encryptedData: e.detail.encryptedData,
          sessionKey: sessionKey,
          iv: e.detail.iv,
          openid: openid
        },
        success: (res) => {
          wx.hideLoading();
          this_.setData({
            show: false,
          })
          if (res.data.code == 200) {
            wx.setStorageSync('wxPhone', res.data.phoneNumber)
            this_.setData({
              wxPhone: res.data.phoneNumber,
              'wxUser.wxPhone': res.data.phoneNumber
            })
            let user = wx.getStorageSync('wxUser')
            user.wxPhone = res.data.phoneNumber
            wx.setStorageSync('wxUser', user)
            //如果sender存在 即是通过分享页面进入并登录、授权手机号 就可以成为体验会员
            let sender = wx.getStorageSync('sender');
            if (sender != null) {
              //receiver为this_.data.wxuser.id
              wx.request({
                url: api.saveVipShare,
                method: 'POST',
                data: {
                  sender: sender,
                  receiver: this_.data.wxUser.id
                },
                success: (res) => {

                }
              })
            }
            wx.showToast({
              title: res.data.msg
            })
          } else {
            wx.showToast({
              title: res.data.msg
            })
          }

        },
        fail: (res) => {
          wx.hideLoading();
        }
      })
    },
    getUserProfile(e) {
      var this_ = this;
      wx.showLoading()
      var param = {};
      param.desc = "用户信息展示"
      //调用微信api 获取个人信息
      wx.getUserProfile({
        desc: "用户信息展示",
        success: (res) => {
          this_.setUserInfo(res)
        },
        fail: (res) => {
          wx.hideLoading();
        }
      })
    },
    setUserInfo(res) {
      var this_ = this;
      wx.hideLoading();
      console.log("userInfo", res.userInfo)
      //将个人信息 传给后台
      let userInfo = res.userInfo;
      this_.setData({
        userInfo: userInfo,
        onConfirmId: 1
      })
      console.log('onConfirmId',this_.data.onConfirmId)
      var openid = wx.getStorageSync('openid');
      userInfo.openid = openid
      wx.request({
        url: api.saveBaseInfo,
        method: 'POST',
        data: userInfo,
        success: (res) => {
          wx.hideLoading();
          if (res.data.code == 200) {
            console.log("wxUser", res.data.wxUser)
            wx.setStorageSync('wxUser', res.data.wxUser)
            this_.setData({
              wxUser: res.data.wxUser,
              wxPhoneCSS: true,
            })
          } else {
            this_.setData({
              wxPhoneCSS: true
            })
          }
        },
      })
    },
    onClose() {
      this.setData({
        show: false,
      })
      this.triggerEvent('onAuthClose')
    }
  }
})